from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Collection(models.Model):
    teacher = models.ForeignKey(User, limit_choices_to = {'groups__in': [1]}) #Teachers group
    course = models.ForeignKey('Course')
    name = models.CharField(max_length = 50)
    description = models.TextField()
    creation_date = models.DateTimeField(auto_now_add = True)
    last_modified = models.DateTimeField(auto_now = True)
    students = models.ManyToManyField(User, limit_choices_to = {'groups__in': [2]}, related_name='Collecion_students') # Students group
    tags = models.ManyToManyField('Tag')
    
    class Meta:
        unique_together = (('teacher', 'name'),)
        
    def __unicode__(self):
        return self.name

class Tag(models.Model):
    name = models.CharField(max_length = 30)
    
    def __unicode__(self):
        return self.name

class Content(models.Model):
    MEDIA_TYPE = (
        ('audio/ogg', 'Audio Ogg'),
        ('audio/mpeg', 'Audio MP3'),
        ('video/ogg', 'Video Ogg'),
        ('text/plain', 'Text'),
        ('image/jpeg', 'Photo'),
        ('text/url', 'URL')
                  )
    user = models.ForeignKey(User)
    collection = models.ForeignKey('Collection')
    type = models.CharField(max_length = 10, choices = MEDIA_TYPE)
    name = models.CharField(max_length = 50)
    description = models.CharField(max_length = 140)
    resource = models.TextField()
    tags = models.ManyToManyField('Tag')
    creation_date = models.DateTimeField(auto_now_add = True)
    
    class Meta:
        unique_together = (('user', 'name', 'type'),)
        
    def __unicode__(self):
        return self.name

class Course(models.Model):
    #http://www.djangofoo.com/tag/limit_choices_to
    teacher = models.ForeignKey(User, limit_choices_to = {'groups__in': [1]}) #Teachers group
    name = models.CharField(max_length = 50)
    year = models.IntegerField(max_length = 4)
    students = models.ManyToManyField(User, limit_choices_to = {'groups__in': [2]}, related_name='course_students') # Students group
    active = models.BooleanField(verbose_name = 'Active')
    
    def __unicode__(self):
        return self.name
